package com.zdx.yswl.view.dialog;

import com.zdx.yswl.R;
import com.zdx.yswl.base.KActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ConfirmDialog extends Dialog implements android.view.View.OnClickListener {

	public static final String TAG = ConfirmDialog.class.getSimpleName();

	private static ConfirmDialog share;

	public static ConfirmDialog share(KActivity __activity, int __id, ConfirmDialogDelegate __delegate) {
		if (share == null) {
			share = new ConfirmDialog(__activity);
		}
		share.mId = __id;
		share.mDelegate = __delegate;
		return share;
	}

	public interface ConfirmDialogDelegate {
		public void onClick_ConfirmDialog(int __id, int __position);
	}

	/**
	 * 样式清单类
	 */
	public static final class $_Style {

		/**
		 * 标题字体大小
		 */
		public static int TITLE_TEXT_SIZE = 24;

		/**
		 * 标题背景
		 */
		public static int TITLE_BG_RESOURCE = Color.TRANSPARENT;

		/**
		 * 标题字体颜色
		 */
		public static int TITLE_TEXT_COLOR = Color.DKGRAY;

		/**
		 * 内容字体大小
		 */
		public static int CONTENT_TEXT_SIZE = 20;

		/**
		 * 内容字体颜色
		 */
		public static int CONTENT_TEXT_COLOR = Color.DKGRAY;

		/**
		 * 按钮字体大小
		 */
		public static int BUTTON_TEXT_SIZE = 20;

		/**
		 * 背景
		 */
		public static int LAYOUT_BG = Color.WHITE;

		/**
		 * 按钮之间分割线的颜色
		 */
		public static int BUTTON_SPLIT_LINE_COLOR = Color.WHITE;

		/**
		 * 按钮字体的颜色
		 */
		public static int BUTTON_TEXT_COLOR = Color.DKGRAY;

		/**
		 * 只有一个按钮时的背景
		 */
		public static int SINGLE_BUTTON_BG_RESOURCE = R.drawable.button_normal;

		/**
		 * 多个按钮时，最左面按钮的背景
		 */
		public static int LEFT_BUTTON_BG_RESOURCE = R.drawable.button_normal_one;

		/**
		 * 多个按钮时，最右边按钮的背景
		 */
		public static int RIGHT_BUTTON_BG_RESOURCE = R.drawable.button_normal_one;

		/**
		 * 多个按钮时，中间按钮的背景
		 */
		public static int CENTER_BUTTON_BG_RESOURCE = R.drawable.button_normal_one;
	}

	private int mId;
	private String mTitle;
	private String mContent;
	private String[] mStrArrs;
	private ConfirmDialogDelegate mDelegate;

	private ConfirmDialog(KActivity __activity) {
		super(__activity, R.style.custom_dialog);
		setCanceledOnTouchOutside(false);
		setCancelable(false);
	}

	public static void showDialog(String __title, String __content, String... __strArr) {
		if (share == null) {
			return;
		}
		try {
			share.mTitle = __title;
			share.mContent = "    " + __content;
			share.mStrArrs = __strArr;
			share.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeDialog() {
		if (share == null) return;
		try {
			share.dismiss();
			share = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.confirm_dialog);

		super.findViewById(R.id.main_layout).setBackgroundResource($_Style.LAYOUT_BG);

		TextView __titleTv = (TextView) super.findViewById(R.id.title_tv);
		__titleTv.setText(mTitle);
		__titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, $_Style.TITLE_TEXT_SIZE);
		__titleTv.setBackgroundResource($_Style.TITLE_BG_RESOURCE);
		__titleTv.setTextColor($_Style.TITLE_TEXT_COLOR);

		TextView __contentTv = (TextView) super.findViewById(R.id.content_tv);
		__contentTv.setText(mContent);
		__contentTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, $_Style.CONTENT_TEXT_SIZE);
		__contentTv.setTextColor($_Style.CONTENT_TEXT_COLOR);

		LinearLayout __bottomLayout = (LinearLayout) super.findViewById(R.id.bottom_layout);

		int __size = mStrArrs.length;
		if (__size == 1) {
			Button __btn = createSingleBtn(mStrArrs[0]);
			__btn.setTag(0);
			__bottomLayout.addView(__btn);
			__btn.setOnClickListener(this);
		} else if (__size > 1) {
			for (int i = 0; i < __size; i++) {
				String __text = mStrArrs[i];
				Button __btn = null;
				if (i == 0) {
					__btn = createLeftBtn(__text);
				} else if (i == __size - 1) {
					__btn = createRightBtn(__text);
				} else {
					__btn = createCenterBtn(__text);
				}
				__bottomLayout.addView(__btn);
				__btn.setTag(i);
				__btn.setOnClickListener(this);

				if (i < __size - 1) {
					__bottomLayout.addView(createLine());
				}
			}
		}
	}

	@Override
	public void onClick(View __v) {
		if (mDelegate != null) {
			mDelegate.onClick_ConfirmDialog(mId, (Integer) __v.getTag());
		}
		if (this == share) {
			closeDialog();
		} else {
			this.dismiss();
		}
	}

	protected View createLine() {
		LinearLayout.LayoutParams __params = new LinearLayout.LayoutParams(1, LayoutParams.MATCH_PARENT);
		View __view = new View(getContext());
		__view.setLayoutParams(__params);
		__view.setBackgroundColor($_Style.BUTTON_SPLIT_LINE_COLOR);
		return __view;
	}

	protected Button createSingleBtn(String __text) {
		LinearLayout.LayoutParams __params = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
		Button __btn = new Button(getContext());
		__btn.setLayoutParams(__params);
		__btn.setText(__text);
		__btn.setTextColor($_Style.BUTTON_TEXT_COLOR);
		__btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, $_Style.BUTTON_TEXT_SIZE);
		__btn.setBackgroundResource($_Style.SINGLE_BUTTON_BG_RESOURCE);
		return __btn;
	}

	protected Button createLeftBtn(String __text) {
		LinearLayout.LayoutParams __params = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
		Button __btn = new Button(getContext());
		__btn.setLayoutParams(__params);
		__btn.setText(__text);
		__btn.setTextColor($_Style.BUTTON_TEXT_COLOR);
		__btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, $_Style.BUTTON_TEXT_SIZE);
		__btn.setBackgroundResource($_Style.LEFT_BUTTON_BG_RESOURCE);
		return __btn;
	}

	protected Button createRightBtn(String __text) {
		LinearLayout.LayoutParams __params = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
		Button __btn = new Button(getContext());
		__btn.setLayoutParams(__params);
		__btn.setText(__text);
		__btn.setTextColor($_Style.BUTTON_TEXT_COLOR);
		__btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, $_Style.BUTTON_TEXT_SIZE);
		__btn.setBackgroundResource($_Style.RIGHT_BUTTON_BG_RESOURCE);
		return __btn;
	}

	protected Button createCenterBtn(String __text) {
		LinearLayout.LayoutParams __params = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
		Button __btn = new Button(getContext());
		__btn.setLayoutParams(__params);
		__btn.setText(__text);
		__btn.setTextColor($_Style.BUTTON_TEXT_COLOR);
		__btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, $_Style.BUTTON_TEXT_SIZE);
		__btn.setBackgroundResource($_Style.CENTER_BUTTON_BG_RESOURCE);
		return __btn;
	}

}
